package com.zsy.petshop_01.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsy.petshop_01.dao.mapper.PetUserMapper;
import com.zsy.petshop_01.model.entity.LoginInfo;
import com.zsy.petshop_01.model.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserManager {
    @Autowired
    PetUserMapper userMapper;

    public List<User> selectAllUser(){
        return userMapper.selectList(null);
    }

    public User login(LoginInfo loginInfo){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, loginInfo.getUsername());
        return userMapper.selectOne(wrapper);
    }

    public void insert(User user){
        userMapper.insert(user);
    }

    public void deleteByName(String name){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getName, name);
        userMapper.delete(wrapper);
    }

    public User selectById(Integer userId){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, userId);
        return userMapper.selectOne(wrapper);
    }

    public void updateById(User user){
        userMapper.updateById(user);
    }
}
